package com.atguigu.gulimalltestssoclient.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author houChen
 * @date 2022/1/1 14:57
 * @Description:
 */
@Controller
public class HelloController {

    @Value("${sso.server.url}")
    private String ssoServerUrl;

    @ResponseBody
    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }

    /**
     * @Description:
     * @param model
     * @param session
     * @param token          ssoserver服务器登录成功，跳转回来会携带token
     * @author  houChen
     * @date  2022/1/1 22:03
     */
    @GetMapping("/boss")
    public String employees(Model model, HttpSession session,@RequestParam(value = "token",required = false) String token) throws Exception {
        if(!StringUtils.isEmpty(token)){
            //ssoserver服务器登录成功，跳转回来
            //去ssoserver获取当前token真正对应的用户信息
            //去ssoserver获取当前token真正对应的用户信息
            RestTemplate restTemplate = new RestTemplate();
            ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://ssoserver.com:8880/userInfo?token=" + token, String.class);
            session.setAttribute("loginUser",responseEntity.getBody());
        }
        Object loginUser = session.getAttribute("loginUser");
        if(loginUser==null){
            //未登录，则跳转到登录页
            return "redirect:"+ssoServerUrl + "?redirect_url=http://client2.com:8882/boss" ;
        }else{
            ArrayList<String> emps = new ArrayList<>();
            emps.add("后晨");
            model.addAttribute("emps",emps);
            return "list";
        }
    }
}